from rest_framework.views import APIView
from tools.lib.util import Util
from main.lib.verify import *
from main.lib.save import *


class ShareMsgSaveView(APIView):

    def post(self, request, format=None):
        data = request.data
        if Util.check_data_key(data,["oid","msg_body"]):
            verify,obj = ShareMsgVerify.check(data)
            if verify:
                save_result = MsgSave.save(
                        oid=data["oid"],
                        msg=obj)
                ret = {"text":save_result[1]}
                if save_result[0]:
                    return Util.say_yes(ret)
                return Util.say_no(ret)
            else:
                return Util.say_no({"text":"消息无法被解析!","detail":'请发送符合互联互通标准的消息!'})
        return Util.say_no({"text":"消息格式不正确!","detail":'正确的请求需要包含："oid", "msg_body"'})

class RequestMsgSaveView(APIView):

    def post(self, request, format=None):
        data = request.data
        if Util.check_data_key(data,["oid","type_index","msg_body"]):
            verify,obj = RequestMsgVerify.check(data)
            if verify:
                save_result = MsgSave.save(
                        oid=data["oid"],
                        msg=obj)
                ret = {"text":save_result[1]}
                if save_result[0]:
                    return Util.say_yes(ret)
                return Util.say_no(ret)
            else:
                return Util.say_no({"text":"消息无法被解析!","detail":'请发送符合互联互通标准的消息!'})
        return Util.say_no({"text":"消息格式不正确!","detail":'正确的请求需要包含："oid", "request_type", "msg_body"'})
